import vue from 'vue';
import {
	Message,
	MessageBox
} from 'element-ui';
vue.component(Message.name, Message);
export default {
	install(Vue, options) {
		 Vue.prototype.getBase64Image=function(img) { //转base64
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var dataURL = canvas.toDataURL("image/png");
        return dataURL
        // return dataURL.replace("data:image/png;base64,", "");
    }

		Vue.prototype.shuzu = function(val) {
			var obj={
				mulu : ['adminItemType', 'commonItemType', 'useLevel'],
				shishi : ['svcType', 'delyType', 'handType', 'legObjType', 'perObjType', 'legSubType', 'perSubType'],
				kuozhan : ['appType', 'handScope', 'sysJoinType'],
				cailiao:['format']
			}
			for(var i in obj){
				for(var a in obj[i]){
					var data = obj[i][a]
					if (val == data) {
						return true
					}
				}

			}
			return false;
		}
		Vue.prototype.sjc = function() { //时间戳
			return Date.parse(new Date())
		}
		Vue.prototype.request = function(url, data, fun) { //ajax 可以汉字
			var ajax = null;
			try {
				ajax = new XMLHttpRequest()
			} catch (e) {
				ajax = new ActiveXObject("Microsoft.XMLHTTP")
			}
			ajax.responseType = "blob";
			ajax.open("post", url, true)
			ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded")
			ajax.setRequestHeader('Authorization', localStorage.getItem('authorization'));
			ajax.onload = function() {
				if (this.status == 200) {
					var blob = this.response;
					var eleLink = document.createElement('a');
					eleLink.download = data;
					eleLink.style.display = 'none';
					eleLink.href = URL.createObjectURL(blob);
					document.body.appendChild(eleLink);
					eleLink.click();
					document.body.removeChild(eleLink);
				} else if (this.status == 401) {
					MessageBox.confirm('登录过期，跳转到登录页?').then(action => {
						localStorage.removeItem('authorization');
						router.push('/login');
					});
				}
			}
			ajax.send()
			/* ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.status == 200) {

						fun(JSON.parse(ajax.responseText))

					}
				}
			} */
		}
		Vue.prototype.shiJc = function(timestamp) { //时间戳转时间 时分秒
			if (timestamp && timestamp.length < 11) {
				timestamp = timestamp * 1000
			}
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-';
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			var D = date.getDate() + ' ';
			var h = date.getHours();
			var m = date.getMinutes();
			var s = date.getSeconds();
			return Y + M + D + (h < 10 ? ('0' + h) : h) + ':' + (m < 10 ? ('0' + m) : m) + ':' + (s < 10 ? ('0' + s) : s);
		}
		Vue.prototype.riqi = function(timestamp) { //时间戳转时间 年月日
			if (timestamp && timestamp.length < 11) {
				timestamp = timestamp * 1000
			}
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-';
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			var D = date.getDate()
			return Y + (M < 0 ? ('0' + M) : M) + (D < 10 ? ('0' + D) : D);
		}

		Vue.prototype.extend = function(o, n) { //合并数组
			for (var p in n) {
				if (n.hasOwnProperty(p) && (!o.hasOwnProperty(p)))
					o[p] = n[p];
			}
			return o;
		}
		Vue.prototype.uniqjiandan = function(array) { //简单数组去重
			var temp = []; //一个新的临时数组
			for (var i = 0; i < array.length; i++) {
				if (temp.indexOf(array[i]) == -1) {
					temp.push(array[i]);
				}
			}
			return temp;
		}
		Vue.prototype.uniq = function(array) { //数组去重 - json 对象
			var temp = [];
			var index = [];
			var l = array.length;
			for (var i = 0; i < l; i++) {
				for (var j = i + 1; j < l; j++) {
					if (array[i].id === array[j].id) {
						i++;
						j = i;
					}
				}
				temp.push(array[i]);
				index.push(i);
			}
			return temp;
		}
		Vue.prototype.bendi = function(r) {
			var obj = {};
			obj.list = [{
				id: "1",
				tijiao: true,
				token: localStorage.getItem('authorization'),
				items: [{
					itemCode: r.itemCode,
					itemName: r.itemName
				}],
			}]
			var formInputList = {
				itemCode: r.itemCode,
				itemName: r.itemName,
				taskCode: r.taskCode ? r.taskCode : '',
				taskId: r.taskId ? r.taskId : ''
			}
			var type = r.taskCode ? "?type=revise" : ''
			sessionStorage.setItem("section", JSON.stringify(obj));
			sessionStorage.setItem("itemObj", JSON.stringify(formInputList));
			setTimeout(function() {
				window.open('http://' + window.location.host + '/yulan/xianxia/shoujian.html' + type)
			}, 1000)
		}
		Vue.prototype.feiling = function(e, msg) { //验证非负数
      if(e.target.value){
          let boolen = new RegExp("^[0-9][0-9]*$").test(e.target.value)
          if (!boolen) {
            this.$message.warning(msg)
            e.target.value = 0
          }
        }
		}

	}
}
